<template>
	<view class="mine-page page">
		<CustomNav title="野生动物救助" :isBack="true"></CustomNav>
		<view class="tips-block">
			<view class="iconfont icon-a-tishi2">
				<text class="tips-text">
					提示：请如实填写信息，若因填写信息不真实导致其他问题的，后果将由您自行承担。
				</text>
			</view>
		</view>
		<view class="info-block">
			<view class="info-list">
				<view class="form-item">
					<view class="name-item">
						<view class="must">*</view>
						<view class="name">种类</view>
					</view>
					<view class="form-content">
						<picker @change="bindTypeChange" range-key="name" :value="industryIndex" :range="typeList"
							class="form-crl1">
							{{typeIndex==-1 ? '请选择野生动物种类':typeList[typeIndex].name}}
						</picker>
						<view class="right">
							<view class="iconfont icon-gengduo"></view>
						</view>
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="must">*</view>
						<view class="name">位置</view>
					</view>
					<view class="form-content" @click="chooseLocation">
						<input v-model="form.position" :disabled="true" class="form-crl1"
							placeholder-style="color:#000;" placeholder-class="input-placeholder"
							placeholder="请选择野生动物位置" />
						<view class="right">
							<view class="iconfont icon-gengduo"></view>
						</view>
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="must">*</view>
						<view class="name">具体位置</view>
					</view>
					<view class="form-content">
						<input placeholder="请输入具体位置..." v-model="form.specificPosition"
							placeholder-class="input-placeholder" placeholder-style="color:#000;" class="form-crl"
							maxlength="40" />
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="must">*</view>
						<view class="name">野生动物情况</view>
					</view>
					<view class="form-content">
						<textarea placeholder="请简述野生动物情况..." v-model="form.animalSituation" style="height: 200rpx;"
							placeholder-class="input-placeholder" placeholder-style="color:#000;" maxlength="300" />
					</view>
				</view>
				<view class="info-list">
					<view class="info-item">
						<view class="must"></view>
						<view class="name">图片/视频上传({{form.pic.length+form.video.length}}/5)</view>
					</view>
					<view class="upload">
						<view v-for="(item,index) in form.pic" class="image-item">
							<image mode="aspectFit" :src="item" @click="previewImg(item)" class="img">
							</image>
							<view @click="closeImg(index,1)" style="display: inline;">
								<view class="iconfont">
									<image src="/static/home/delete.png" class="delete"></image>
								</view>
							</view>
						</view>
						<view v-for="(item,index) in form.video" class="image-item">
							<video id="myVideo" class="img" style="width: 190rpx;height: 200rpx;border-radius: 20rpx;"
								:src="item" @error="videoErrorCallback" show-fullscreen-btn></video>
							<view @click="closeImg(index,2)" style="display: inline;">
								<view class="iconfont">
									<image src="/static/home/delete.png" class="delete"></image>
								</view>
							</view>
						</view>
						<view class="upload-content" v-if="(form.pic.length+form.video.length)<5" @click="chooseMedia">
							<image src="/static/home/upload.png" class="upload"></image>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="btn-item">
			<view class="btn">
				<view class="btn normal" @click="submit"> 提交</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		validate
	} from '@/util/validate.js'
	import {
		getCurrentTime
	} from '@/util/util.js'
	import {
		getStorage,
		showToast
	} from '@/util/auth';
	import {
		addAndUpdateAnimalRescue,
		getAnimalType
	} from '@/api/animal.js'
	import config from '@/config.js';
	export default {
		data() {
			return {
				form: {
					type: 0,
					position: '',
					specificPosition: '',
					animalSituation: '',
					pic: [],
					video: [],
					latitude: '',
					longitude: ''
				},
				rules: {
					type: {
						name: '野生动物类型',
						required: true,
						validator: function(value, form, callback) {
							if (value == 0) {
								callback('请选择野生动物类型')
							}
							callback();
						}
					},
					position: {
						name: '位置',
						required: true,
						validator: function(value, form, callback) {
							if (value == 0) {
								callback('请选择位置')
							}
							callback();
						}
					},
					specificPosition: {
						name: '具体位置',
						required: true
					},
					animalSituation: {
						name: '野生动物情况',
						required: true
					},
				},
				typeIndex: -1,
				typeList: []
			}
		},
		onLoad() {},
		methods: {
			videoErrorCallback: function(e) {
				uni.showModal({
					content: e.target.errMsg,
					showCancel: false
				})
			},
			bindTypeChange(e) {
				this.typeIndex = e.detail.value
				this.form.type = this.typeList[this.typeIndex].id
			},
			//选择位置
			chooseLocation() {
				uni.chooseLocation({
					latitude: this.$store.getters.position.latitude,
					longitude: this.$store.getters.position.longitude,
					success: (e) => {
						this.form.position = e.address;
						this.form.latitude = e.latitude;
						this.form.longitude = e.longitude;
					},
				})
			},
			getTypeList() {
				getAnimalType().then(res => {
					this.typeList = res
				}).finally(() => {
					uni.stopPullDownRefresh();
				})
			},
			submit() {
				let form = JSON.parse(JSON.stringify(this.form));
				if (validate(form, this.rules)) {
					addAndUpdateAnimalRescue(form).then(res => {
						uni.showToast({
							title: '提交成功',
							icon: 'none',
							duration: 1000,
							mask: true,
							success: function() {
								setTimeout(function() {
									uni.navigateBack();
								}, 1000)
							},
						})
					}).finally(() => {
						uni.stopPullDownRefresh();
					})
				}
			},
			chooseMedia(e) {
				let login = getStorage('login');
				let token = getStorage('token');
				uni.chooseMedia({
					maxDuration: 60,
					count: 5 - this.form.pic.length,
					mediaType: ['image', 'video'],
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						if (res.length < 2) { // 小于2则没有选择图片
							uni.hideLoading()
							return
						}
						let filePath = res.tempFiles[0].tempFilePath;
						let that1 = this;
						uni.showLoading({
							title: '上传中',
							mask: true
						})
						uni.uploadFile({
							url: config.requestUrl + '/sbbz/file/uploadApi', //post请求的地址
							filePath: filePath,
							name: 'file',
							header: {
								'authori-zation': token
							},
							success: (myres) => {
								uni.hideLoading()
								const data = JSON.parse(myres.data);
								if (data.code == 200) {
									console.log(data)
									if (data.data.fileType == 1) {
										this.form.pic.push(data.data.url)
									} else if (data.data.fileType == 2) {
										this.form.video.push(data.data.url)
									} else if (data.data.fileType == 3) {
										this.form.audio.push(data.data.url)
									} else if (data.data.fileType == 4) {
										this.form.PDF.push(data.data.url)
									}

								} else {
									uni.hideLoading()
									showToast('上传失败');
								}
							}
						})
						// })
					}
				})
			},
			// 删除图片
			closeImg(index, e) {
				var that = this;
				uni.showModal({
					title: '提示',
					content: '是否删除当前文件',
					success: function(res) {
						if (res.confirm) {
							if (e == 1) {
								that.form.pic.splice(index, 1);
							} else if (e == 2) {
								that.form.video.splice(index, 1);
							} else if (e == 3) {
								that.form.audio.splice(index, 1);
							} else if (e == 4) {
								that.form.PDF.splice(index, 1);
							}


						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},

		},
		onShow() {
			this.getTypeList()
		}
	}
</script>

<style lang="scss">
	.uni-date-x {
		color: #000 !important;
	}

	.uni-date-x--border {
		border: none !important;
	}

	.info-list {
		display: flex;
		flex-direction: column;
		border-bottom: 1px solid #F7F7F7;
		padding-bottom: 20rpx;
		margin-top: 20rpx;

		.img-item {
			display: flex;
			margin-top: 20rpx;

			.obverse-item {
				width: calc((100% - 30rpx) /2);
				margin-right: 30rpx;

				.upload-img {
					width: 100%;
					border-radius: 10rpx;
					height: 200rpx !important;
				}
			}

			.obverse-item:last-child {
				margin-right: 0;
			}
		}

		.info-item {
			display: flex;
			align-items: center;

			.must {
				color: #EA0000;
			}

			.formwork {
				color: #027BFF;
			}
		}

		.upload {
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;

			.image-item {
				margin-top: 20rpx;
				margin-right: 20rpx;
				display: flex;
				flex-direction: row;
				// box-sizing: border-box;
				// border: 1px solid #efefef;

				.img {
					width: 190rpx;
					height: 200rpx;
					border-radius: 20rpx;
				}

				.iconfont {
					.delete {
						width: 20px;
						height: 20px;
						position: absolute;
						margin-left: -30rpx;
					}
				}
			}

			.image-item:nth-child(3n) {
				margin-right: 0;
			}

			.delete-item {
				width: 100%;
				display: flex;
				justify-content: flex-end;

				.delete {
					width: 20px;
					height: 20px;
					position: absolute;
				}
			}

			.image-item1 {
				display: flex;
				flex-direction: row;

				.delete {
					margin-top: 10rpx;
					margin-left: -40rpx;
					width: 20px;
					height: 20px;
					position: absolute;
				}
			}

			.upload-content {
				position: relative;
				background: #fafafa;
				border-radius: 5px;
				width: 200rpx;
				height: 200rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				margin-top: 20rpx;

				.title {
					position: absolute;
					top: 50%;
					left: 50%;
				}

				.upload {
					width: 90rpx;
					height: 90rpx;
				}
			}
		}

		.form-content {
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			margin-top: 20rpx;

			.check {
				transform: scale(0.7);
			}

			.form-ctrl {
				height: 80rpx;
				line-height: 80rpx;
				flex-grow: 1;
				color: #767676;
			}

			.arrow {
				flex-grow: 0;
				width: 40rpx;
				height: 40rpx;
			}
		}

		.radio-list {
			.radio-item {
				margin-top: 30rpx;

				.check {
					transform: scale(0.7);
				}
			}
		}

		.info-crl {
			margin-top: 30rpx;
		}
	}

	.mine-page {
		overflow: hidden;

		.tips-block {
			background-color: #D6E0FF;
			color: #0256FF;
			width: calc(100% - 60rpx);
			display: flex;
			margin-top: 26%;
			margin-left: 30rpx;
			border-radius: 20rpx;
			padding: 30rpx;

			.iconfont {
				line-height: 60rpx;

				.tips-text {
					margin-left: 20rpx;
				}
			}
		}

		.info-block {
			padding: 30rpx;
			background-color: #F7F6F9;

			.info-list {
				background-color: #fff;
				border-radius: 20rpx;
				padding: 30rpx;
				margin-bottom: 20rpx;

				.form-item:last-child {
					border-bottom: none;
				}

				.form-item {
					display: flex;
					flex-direction: column;
					border-bottom: 1px solid #F7F7F7;
					padding-bottom: 30rpx;
					margin-top: 30rpx;

					.name-item {
						display: flex;
						align-items: center;
						margin-bottom: 30rpx;

						.must {
							color: #EA0000;
						}

						.name {
							font-size: 30rpx;
						}
					}

					.form-content {
						display: flex;
						align-items: center;
						justify-content: space-between;
						width: 100%;

						.form-crl1 {
							width: calc(100% - 80rpx);
							font-size: 30rpx;
							overflow: hidden;
							text-overflow: ellipsis;
							white-space: nowrap;
						}

						.form-crl {
							width: 100%;
						}

						.right {
							display: flex;
							align-items: center;

							.iconfont {
								color: #CDCDCD;
								font-size: 32rpx;
							}

							.distance {
								color: #767676;
								font-size: 28rpx;
							}
						}
					}
				}
			}
		}

		.btn-item {
			display: flex;
			justify-content: center;
			margin-bottom: 40rpx;
		}
	}
</style>